using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Codes.Models;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Codes.Controllers;

public class StudentsController : Controller
{
    private readonly StudentInfoDbContext? _db;
    public StudentsController()
    {
        _db = new StudentInfoDbContext();
    }
    /// <summary>
    /// 显示学生列表页面，支持搜索功能
    /// </summary>
    /// <param name="searchString">搜索关键词</param>
    /// <returns>学生列表视图</returns>
    public IActionResult Index(string searchString)
    {
        if (_db == null)
        {
            return Content("数据库上下文为空");
        }
        var students = _db.Students.AsQueryable();
        if (!String.IsNullOrEmpty(searchString))
        {
            students = students.Where(s => s.StudentName.Contains(searchString) ||
                                        s.Id.ToString().Contains(searchString) ||
                                        s.Address.Contains(searchString));
        }
        return  View(students.ToList());
    }

    // if (_db != null)
    //     {
    //         return Content("数据库上下文为空");
    //     }
    //     var students = _db.Students.AsQueryable();
    //     if (!String.IsNullOrEmpty(searchString))
    //     {
    //         students = students.Where(s => s.StudentName.Contains(searchString) ||
    //                                     s.Id.ToString().Contains(searchString) ||
    //                                     s.Address.Contains(searchString));
    //     }
    public IActionResult Add()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Add(Student student)
    {
        if (ModelState.IsValid)
        {
            if (_db == null)
            {
                return Content("数据库上下文是空的");
            }
            _db.Students.Add(student);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(student);
    }
    public IActionResult Edit(int Id)
    {
        var res = _db?.Students.FirstOrDefault(x => x.Id == Id);
        return View(res);
    }

    [HttpPost]
    /// <summary>
    /// 处理编辑学生表单提交
    /// </summary>
    /// <param name="student">学生信息</param>
    /// <returns>成功则重定向到列表页，失败则返回编辑页</returns>
    public IActionResult Edit(Student student)
    {
        if (ModelState.IsValid)
        {
            if (_db == null)
            {
                return Problem("数据库上下文是空的");
            }
            //编辑代码最主要的部分
            var res = _db?.Students.Find(student.Id);
            if (res != null)
            {
                res.StudentName = student.StudentName;
                res.Address = student.Address;
                res.Age = student.Age;
                _db?.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        return View(student);
    }
    public IActionResult Delete(int Id)
    {
        var Del = _db?.Students.FirstOrDefault(x => x.Id == Id);
        return View(Del);
    }
    [HttpPost]
    /// <summary>
    /// 处理删除学生请求
    /// </summary>
    /// <param name="student">学生信息</param>
    /// <param name="Id">学生ID</param>
    /// <returns>重定向到列表页</returns>
    public IActionResult Delete(Student student, int Id)
    {
        if (_db == null)
        {
            return Content("数据为空");
        }
        //
        var Del = _db?.Students.Find(Id);
        if (Del != null)
        {
            _db?.Students.Remove(Del);
            _db?.SaveChanges();
        }
        return RedirectToAction("Index");

    }
}